package com.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author haining
 */
public class SecondFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse1, FilterChain filterChain) throws IOException, ServletException {
        System.out.println(3);
        ServletResponse object = servletResponse1;

        Method method1 = null;
        try {
            method1 = object.getClass().getMethod("getResponse");
            Object servletResponse = method1.invoke(object, new Object[] {});
            Method method2 = servletResponse.getClass().getMethod("getResponse");
            Object parentResponse = method2.invoke(servletResponse, new Object[] {});
            Method method3 = parentResponse.getClass().getMethod("getStatus");

            int httpStatus = (Integer) method3.invoke(parentResponse, new Object[] {});
            System.out.println("status："+httpStatus);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


        filterChain.doFilter(servletRequest, servletResponse1);
        System.out.println(4);

        Method methodq = null;
        try {
            methodq = object.getClass().getMethod("getResponse");
            Object servletResponse = methodq.invoke(object, new Object[] {});
            Method method2 = servletResponse.getClass().getMethod("getResponse");
            Object parentResponse = method2.invoke(servletResponse, new Object[] {});
            Method method3 = parentResponse.getClass().getMethod("getStatus");

            int httpStatus = (Integer) method3.invoke(parentResponse, new Object[] {});
            System.out.println("status："+httpStatus);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {

    }

}
